import {BaseEntity, BaseApi, Result} from "@/api/BaseApi";
import {Request} from "@/api/Request";
import http from "@/api/axios";
import {FormRules} from "element-plus/es";
import {sys_application_name} from "@/api/constant";

class SysMenuApi<T> extends BaseApi<T> {
    API_PREFIX = sys_application_name + '/SysMenuApi';

    getSysMenu() {
        return new Request<Result<SysMenuTree>>().setHttp(() => http.get(this.getUrl('getSysMenu')));
    }
}

export default new SysMenuApi<SysMenu>();

export interface SysMenu extends BaseEntity {
    //菜单ID
    id?: number;
    //
    name?: string;
    //
    path: string;
    //父菜单ID
    parentId?: number;
    //
    icon?: string;

    menuType?: string;

    sort?: number;
}

export class SysMenuTree implements SysMenu {
    //菜单ID
    id?: number;
    //
    name?: string;
    //
    path!: string;
    //父菜单ID
    parentId?: number;

    icon?: string;

    children?: SysMenuTree[];

    sort?: number;

    isHeaderActive = false;  //header 的一级菜单是否选中

    isNavActive = false;

    menuType?: string;
}

//规则校验
export const SysMenuRules = (sysMenuTree:SysMenuTree) => {
    let rules:FormRules = {
        name: [
            {required: true, min: 2, max: 20, message: '请输入2-20位的菜单名称', trigger: 'change'},
        ],
        icon: [
            {
                validator: (rule, value, callback, source, options) => {
                    if (!sysMenuTree.name) {
                        console.log(sysMenuTree)
                        console.log('1111')
                        callback(new Error('错误选择'));
                    }
                    callback();
                }, message: '请选择图标2', trigger: 'change'
            },
        ]
    }
    return rules;
}

/*export const SysMenuRules: FormRules = {
    name: [
        {required: true, min: 2, max: 10,  message: '请输入2-10位的菜单名称', trigger: 'change'},
    ],
    icon:[
        {required: true, message: '请选择图标', trigger: 'change'},
        { validator:(rule, value, callback, source, options) => {
                console.log(rule);
                console.log(value);
                console.log(source);
                console.log(options);
                callback();
            }, message: '请选择图标2', trigger: 'change'},
    ]

}*/
